package interfazeak;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableModel;

import db.UpelaKud;

import Taulak.MTBKonfUpelak;

public class Upela extends JDialog {

	private Konfigurazioa guraso;
	private final JPanel contentPanel = new JPanel();
	private MTBKonfUpelak konfigupelak;
	private JButton gehitu;
	private JButton ezabatu;
	private JTable table ;
	public static void main(String[] args) {
		try {
			final Boolean MODALA = true;

			Upela dialog = new Upela(new Konfigurazioa(), MODALA);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Upela(Konfigurazioa guraso, boolean b) {
		
		this.guraso = guraso;

		setBounds(100, 100, 748, 451);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setTitle("UPELEN KONFIGURAZIOA");
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(90, 100, 350, 250);
		contentPanel.add(scrollPane);

		table = new JTable();
		konfigupelak = new MTBKonfUpelak();
		table.setModel(konfigupelak);

		scrollPane.setViewportView(table);

		gehitu = new JButton("GEHITU");
		gehitu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Upela.this.gehitu();
			}
		});
		gehitu.setBounds(460, 180, 117, 25);
		contentPanel.add(gehitu);

		ezabatu = new JButton("EZABATU");
		ezabatu.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if (table.getSelectedRow() == -1) {
					JOptionPane.showMessageDialog(Upela.this,
							"Lerro bat aukeratu!", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);

				}
				else {
					int konfirmatu = JOptionPane.showConfirmDialog(
							Upela.this, "Ezabatu nahi duzu?");

					if (JOptionPane.OK_OPTION == konfirmatu) {
						Upela.this.ezabatu();
					}
				}
			}
		});
		ezabatu.setBounds(460, 220, 117, 25);
		contentPanel.add(ezabatu);
		
		JButton  bueltabtn= new JButton("KONFIGURAZIOA");
		bueltabtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Upela.this.buelta();
			}
		}); 
		bueltabtn.setBounds(500, 20, 157, 25);
		contentPanel.add(bueltabtn);
		bistaratu();
	}

	protected void buelta() {
		
		Konfigurazioa h = new Konfigurazioa();
		h.setVisible(true);
		this.dispose();
	}

	protected void ezabatu() {
		
		int lerroa = table.getSelectedRow();
		int upela = (int) table.getValueAt(lerroa, 0);
		int edukiera = (int) table.getValueAt(lerroa, 1);
		UpelaKud.ezabatuUpelaKonf(upela);
		konfigupelak.ezabatu(lerroa);
	}

	public void bistaratu() {
		
		ResultSet rs = UpelaKud.lortuUpelak();
		try {
			while (rs.next()) {
				int zenb = rs.getInt("zenbakia");
				int eduki = rs.getInt("edukiera");
				konfigupelak.kargatu(zenb, eduki);

			}
		} catch (SQLException e) {

			e.printStackTrace();
		}
	}

	protected void gehitu() {
		
		JDialog dialog = new GehituUpela(Upela.this, true);
		dialog.setVisible(true);
	}

	public void ezabatutaula() {
	
		
		int ler = table.getRowCount();
		konfigupelak.taulaHasieratu(ler);
	}
}
